package com.zust.ysc012.dao;/*
 *项目名: design
 *文件名: StudentDao
 *创建者: 闫思潮
 *创建时间:30/06/2021 11:07
 *描述: TODO
 */

import com.zust.ysc012.entity.Project;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.zust.ysc012.utility.DBUtil.*;

public class StudentDao {
    public static String select_password_student_sql(String student_number) throws SQLException {
        String sql = "SELECT password\n" +
                "FROM student\n" +
                "WHERE student_number = ?";
        ResultSet rs;
        String password = null;
        Object[] params = new Object[1];
        params[0] = student_number;
        rs = executeSelect(sql, params);
        while (rs.next()) {
            password = rs.getString(1);
        }
        pre.close();
        con.close();
        return password;
    }

    public static void alert_status1_sql(int project_number) throws SQLException {
        String sql = "UPDATE student_project \n" +
                "SET status = 1\n" +
                "WHERE project_number = ?";
        Object[] params = new Object[1];
        params[0] = project_number;
        executeUpdate(sql, params);
    }

    public static void alert_status2_sql(int project_number) throws SQLException {
        String sql = "UPDATE student_project \n" +
                "SET status = 2\n" +
                "WHERE project_number = ?";
        Object[] params = new Object[1];
        params[0] = project_number;
        executeUpdate(sql, params);
    }

    public static void insert_student_account_sql(String account, String password) throws SQLException {
        String sql = "INSERT INTO student (student_number, `password`)\n" +
                "VALUES (?, ?)";
        Object[] params = new Object[2];
        params[0] = account;
        params[1] = password;
        executeUpdate(sql, params);
    }

    public static String select_student_name_sql(String student_number) throws SQLException, UnsupportedEncodingException {
        String sql = "SELECT name\n" +
                "FROM school\n" +
                "WHERE number = ?";
        ResultSet rs;
        String name = null;
        Object[] params = new Object[1];
        params[0] = student_number;
        rs = executeSelect(sql, params);
        while (rs.next()) {
            name = rs.getString(1);
        }
        pre.close();
        con.close();
        return name;
    }

    public static void insert_student_project_sql(String student_number, int project_number, String teacher_number) {
        String sql = "INSERT INTO student_project(student_number, project_number, teacher_number, `status`)\n" +
                "VALUES(?,?,?,?)";
        Object[] params = new Object[4];
        params[0] = student_number;
        params[1] = project_number;
        params[2] = teacher_number;
        params[3] = 0;
        executeUpdate(sql, params);
    }

    public static int select_status_sql(int project_number) throws SQLException {
        String sql = "SELECT status\n" +
                "FROM student_project\n" +
                "WHERE project_number = ?";
        ResultSet rs;
        int status = -1;
        Object[] params = new Object[1];
        params[0] = project_number;
        rs = executeSelect(sql, params);
        while (rs.next()) {
            status = rs.getInt(1);
        }
        pre.close();
        con.close();
        return status;
    }
}
